package com.gl.goods.ui.main.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.gl.goods.R;
import com.gl.goods.base.BaseMvpFragment;
import com.gl.goods.base.MyApplication;
import com.gl.goods.ui.login.activity.LoginActivity;
import com.gl.goods.ui.login.bean.UserInfoBean;
import com.gl.goods.ui.main.activity.MainActivity;
import com.gl.goods.ui.main.mvp.me.UserFragmentContract;
import com.gl.goods.ui.main.mvp.me.UserFragmentPresenter;
import com.gl.goods.ui.user.UserInfoActivity;
import com.gl.library.tool.RxSPTool;
import com.hjq.toast.ToastUtils;
import com.simple.spiderman.SpiderMan;

import org.simple.eventbus.Subscriber;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Time; 2020/11/13
 * Author: hmc
 * Description:
 */
public class UserFragment extends BaseMvpFragment<UserFragmentPresenter> implements UserFragmentContract.View {
    @BindView(R.id.tv_name)
    TextView mUserNameTv;//用户姓名
    @Override
    protected int initLayout() {
        return R.layout.fragment_user;
    }

    @Override
    protected void initView(Bundle savedInstanceState, View view) {
        mPresenter = new UserFragmentPresenter();
        mPresenter.attachView(this);
        /**获取用户信息*/
        mPresenter.getUserInfo();
    }

    @Override
    public void getUserInfoSuccess(UserInfoBean userInfoBean) {
        mUserNameTv.setText(userInfoBean.getData().getName() != null ? userInfoBean.getData().getName() : "没有");
    }

    @Override
    public void logout() {
        MyApplication.getInstance().logout();
        RxSPTool.putInt(mContext, "memberId", 0);
        RxSPTool.putString(mContext, "token", "");
        goTo(LoginActivity.class, new Intent().setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK));
    }

    @Subscriber(tag = "切换我的")
    public void getUserInfo(String data){
        showDialogLoading(R.string.loading);
        /**获取最新用户信息*/
        mPresenter.getUserInfo();
    }

    @OnClick({R.id.tv_login_out,R.id.tv_name})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.tv_login_out:
                /**退出登录*/
                mPresenter.logout();
                break;
            case R.id.tv_name:
                goTo(UserInfoActivity.class);
                break;
        }
    }

    @Override
    public void showLoading() {
        showDialogLoading(R.string.loading);
    }

    @Override
    public void hideLoading() {
        hideDialogLoading();
    }

    @Override
    public void showErrorMessage(String error_message) {
        ToastUtils.show(error_message);
    }
}
